package zyh.xml;


import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import zyh.model.Mp3Info;

public class Mp3ListContentHandler extends DefaultHandler  {

	private List<Mp3Info> infos=null;
	private Mp3Info mp3Info=null;
	private String targetName=null;
	
	
	public Mp3ListContentHandler(List<Mp3Info> infos) {
		super();
		this.infos = infos;
	}

	public List<Mp3Info> getInfos() {
		return infos;
	}

	public void setInfos(List<Mp3Info> infos) {
		this.infos = infos;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		String temp=new String(ch,start,length);
		if(targetName.equals("id"))
		{mp3Info.setId(temp);}
		else if(targetName.equals("mp3.name"))
		{mp3Info.setMp3Name(temp);}
		else if(targetName.equals("mp3.size"))
		{mp3Info.setMp3Size(temp);}
		else if(targetName.equals("lrc.name"))
		{mp3Info.setLrcName(temp);}
		else if(targetName.equals("lrc.size"))
		{mp3Info.setLrcSize(temp);}
		else if(targetName.equals("category")){
			mp3Info.setCategory(temp);
		}
		
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		if(qName.equals("resource"))
		{
			infos.add(mp3Info);
		}
		targetName="";
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		this.targetName=localName;
		if(targetName.equals("resource"))
		{
			mp3Info=new Mp3Info();
			
		}
		//super.startElement(uri, localName, qName, attributes);
	}
	

}
